<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入设备品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备容量" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入设备容量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="额定功率" prop="ratedPower">
        <el-input
          v-model="queryParams.ratedPower"
          placeholder="请输入额定功率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商" prop="operatorId">
        <el-select v-model="queryParams.operatorId" filterable placeholder="请选择运营商">
          <el-option
            v-for="item in operatorList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="安装日期" prop="installTime">
        <el-date-picker clearable
                        v-model="queryParams.installTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择安装日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['smart:subsidiary:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['smart:subsidiary:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['smart:subsidiary:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['smart:subsidiary:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subsidiaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设备名称" align="center" prop="deviceName"/>
      <el-table-column label="设备别名" align="center" prop="nickName"/>
      <el-table-column label="设备品牌" align="center" prop="brand"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="交直流类型" align="center" prop="currentTypeName"/>
      <el-table-column label="设备容量" align="center" prop="capacity"/>
      <el-table-column label="额定功率" align="center" prop="ratedPower"/>
     <!-- <el-table-column label="设备位置" align="center" prop="position"/>-->
      <el-table-column label="设备类型" align="center" prop="deviceTypeName"/>
      <el-table-column label="运营商" align="center" prop="operatorName"/>
     <!-- <el-table-column label="服务费" align="center" prop="serviceFee"/>
      <el-table-column label="电费" align="center" prop="electricCharge"/>-->
      <el-table-column label="安装日期" align="center" prop="installTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName"/>
<!--      <el-table-column label="修改人" align="center" prop="updateName"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdatePort(scope.row)"
                     v-hasPermi="['smart:subsidiary:edit']">配置端口
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['smart:subsidiary:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['smart:subsidiary:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备附属对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <div class="box">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="设备" prop="deviceId">
            <el-select v-model="form.deviceId" filterable style="width: 340px" placeholder="请选择设备">
              <el-option
                v-for="item in iotDeviceList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备别名" prop="nickName">
            <el-input v-model="form.nickName" placeholder="请输入设备别名"/>
          </el-form-item>
          <el-form-item label="设备品牌" prop="brand">
            <el-input v-model="form.brand" placeholder="请输入设备品牌"/>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入地址"/>
          </el-form-item>
          <el-form-item label="设备容量" prop="capacity">
            <el-input type="number" v-model="form.capacity" placeholder="请输入设备容量"/>
          </el-form-item>
          <el-form-item label="额定功率" prop="ratedPower">
            <el-input type="number" v-model="form.ratedPower" placeholder="请输入额定功率"/>
          </el-form-item>
          <el-form-item label="设备位置" prop="position">
            <el-input v-model="form.position" placeholder="请输入设备位置"/>
          </el-form-item>
          <el-form-item label="设备类型" prop="deviceType">
            <el-select v-model="form.deviceType" filterable style="width: 340px" placeholder="请选择设备类型">
              <el-option
                v-for="item in deviceTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="交直流类型" prop="currentType">
            <el-select v-model="form.currentType" filterable style="width: 340px" placeholder="请选择交直流类型">
              <el-option
                v-for="item in currentTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运营商" prop="operatorId">
            <el-select v-model="form.operatorId" filterable style="width: 340px" placeholder="请选择运营商"
                       @change="getStationList">
              <el-option
                v-for="item in operatorList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="站点" prop="stationId">
            <el-select v-model="form.stationId" filterable placeholder="请选择站点" style="width: 340px">
              <el-option
                v-for="item in stationList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="价格类型" prop="priceTypeCode">
            <el-select v-model="form.priceTypeCode" filterable style="width: 340px" placeholder="请选择运营商"
                       @change="getListPriceType">
              <el-option
                v-for="item in priceTypeList"
                :key="item.priceTypeCode"
                :label="item.priceTypeName"
                :value="item.priceTypeCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="服务费" prop="serviceFee">
            <el-input type="number" v-model="form.serviceFee" placeholder="请输入服务费"/>
          </el-form-item>
          <el-form-item label="电费" prop="electricCharge">
            <el-input type="number" v-model="form.electricCharge" placeholder="请输入电费"/>
          </el-form-item>
          <el-form-item label="安装日期" prop="installTime">
            <el-date-picker clearable style="width: 340px"
                            v-model="form.installTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择安装日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 包含设备 -->
    <el-dialog title="端口配置" :visible.sync="portOpen" width="1150px" append-to-body>
      <div class="box_dialog">
        <table-compoment :tableList="tableList" :deviceNickName="deviceNickName" :deviceId="deviceId"></table-compoment>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelPort">确 定</el-button>
        <el-button @click="cancelPort">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listSubsidiary,
    getSubsidiary,
    getSubsidiaryList,
    delSubsidiary,
    addSubsidiary,
    updateSubsidiary,
    getIotDeviceList,
    getOperatorList,
    getStationList,
    getDeviceTypeList,
    getCurrentTypeList
  } from "@/api/smart/subsidiary/subsidiary";
  import {listPriceType} from "@/api/smart/operator/priceType";
  import tableCompoment from "./components/tableCompoment";


  export default {
    name: "Subsidiary",
    components: {tableCompoment},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备附属表格数据
        subsidiaryList: [],
        // 价格类型表格数据
        priceTypeList: [],
        //端口列表
        tableList: [],
        deviceNickName: '',
        deviceId: '',
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        portOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          modelId: null,
          brand: null,
          address: null,
          currentType: null,
          capacity: null,
          ratedPower: null,
          position: null,
          deviceType: null,
          operatorId: null,
          stationId: null,
          serviceFee: null,
          electricCharge: null,
          installTime: null,
          createName: null,
          updateName: null
        },
        //设备列表
        iotDeviceList: [],
        //运营商列表
        operatorList: [],
        //站点列表
        stationList: [],
        //设备类星体列表
        deviceTypeList: [],
        //电流类型列表
        currentTypeList: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          deviceId: [
            {required: true, message: "请选择设备", trigger: "change"}
          ],
          operatorId: [
            {required: true, message: "请选择运营商", trigger: "change"}
          ],
          stationId: [
            {required: true, message: "请选择站点", trigger: "change"}
          ],
          brand: [
            {required: true, message: "请输入设备品牌", trigger: "blur"}
          ],
          capacity: [
            {required: true, message: "请输入设备容量", trigger: "blur"}
            , {pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, message: "请输入正确的设备容量格式"}
          ],
          ratedPower: [
            {required: true, message: "请输入额定功率", trigger: "blur"}
            , {pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, message: "请输入正确的额定功率格式"}
          ],
          serviceFee: [
            {pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, message: "请输入正确的服务费格式"}
          ],
          electricCharge: [
            {pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, message: "请输入正确的电费格式"}
          ],
          nickName: [
            {required: true, message: "请输入设备别名", trigger: "blur"}
          ],
          priceTypeCode: [
            {required: true, message: "请选择价格类型", trigger: "change"}
          ],
        }
      };
    },
    created() {
      /*查询设备列表*/
      this.getIotDeviceList();
      /*查询运营商列表*/
      this.getOperatorList();
      /*设备类型列表*/
      this.getDeviceTypeList();
      /*电流类型列表*/
      this.getCurrentTypeList();
      /*价格类型列表*/
      this.getListPriceType();
      /*查询设备附属列表*/
      this.getList();
    },
    methods: {
      /*查询设备列表*/
      getIotDeviceList() {
        getIotDeviceList().then(response => {
          this.iotDeviceList = response.data;
        });
      },
      /*查询运营商列表*/
      getOperatorList() {
        getOperatorList().then(response => {
          this.operatorList = response.data;
        });
      },
      /*查询运营商站点列表*/
      getStationList() {
        let param = {"id": this.form.operatorId}
        getStationList(param).then(response => {
          this.stationList = response.data;
        });
      },
      /*设备类型列表*/
      getDeviceTypeList() {
        getDeviceTypeList().then(response => {
          this.deviceTypeList = response.data;
        });
      },
      /*电流类型列表*/
      getCurrentTypeList() {
        getCurrentTypeList().then(response => {
          this.currentTypeList = response.data;
        });
      },
      /** 查询设备附属列表 */
      getList() {
        this.loading = true;
        listSubsidiary(this.queryParams).then(response => {
          this.subsidiaryList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      cancelPort() {
        this.deviceNickName = ''
        this.deviceId = ''
        this.tableList = []
        this.portOpen = false;
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          deviceId: null,
          brand: null,
          address: null,
          currentType: null,
          capacity: null,
          ratedPower: null,
          position: null,
          deviceType: null,
          operatorId: null,
          stationId: null,
          serviceFee: null,
          electricCharge: null,
          installTime: null,
          createTime: null,
          createName: null,
          updateTime: null,
          updateName: null,
          nickName: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加设备附属信息";
      },
      /** 配置端口 */
      handleUpdatePort(row) {
        getSubsidiaryList(row.id).then(response => {
          this.deviceNickName = row.nickName
          this.deviceId = row.id
          this.tableList = response.data.data
          this.portOpen = true;
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSubsidiary(id).then(response => {
          let param = {"id": response.data.operatorId}
          getStationList(param).then(response1 => {
            this.stationList = response1.data;
            this.form = response.data;
            this.open = true;
            this.title = "修改设备附属信息";
          });
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSubsidiary(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSubsidiary(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除设备附属信息编号为"' + ids + '"的数据项？').then(function () {
          return delSubsidiary(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        +
          this.download('smart/subsidiary/export', {
            ...this.queryParams
          }, `subsidiary_${new Date().getTime()}.xlsx`)
      },
      /** 查询价格类型列表 */
      getListPriceType() {
        listPriceType().then(response => {
          this.priceTypeList = response.rows;
        });
      },
    }
  };
</script>
<style lang="scss" scoped>
  .box {
    // 必须有高度 overflow 为自动
    overflow: auto;
    height: 600px;
    width: 500px;
    padding: 0px 30px 11px 27px;

    // 滚动条的样式,宽高分别对应横竖滚动条的尺寸
    &::-webkit-scrollbar {
      width: 3px;
    }

    // 滚动条里面默认的小方块,自定义样式
    &::-webkit-scrollbar-thumb {
      background: #BABABA;
      border-radius: 2px;
    }

    // 滚动条里面的轨道
    &::-webkit-scrollbar-track {
      background: transparent;
    }
  }

  .box_dialog {
    // 必须有高度 overflow 为自动
    overflow: auto;
    height: 600px;
    width: 1100px;
    padding: 0px 30px 11px 27px;

    // 滚动条的样式,宽高分别对应横竖滚动条的尺寸
    &::-webkit-scrollbar {
      width: 3px;
    }

    // 滚动条里面默认的小方块,自定义样式
    &::-webkit-scrollbar-thumb {
      background: #BABABA;
      border-radius: 2px;
    }

    // 滚动条里面的轨道
    &::-webkit-scrollbar-track {
      background: transparent;
    }
  }
</style>
